<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link type="text/css" rel="stylesheet" href="static/css/login.css" />
    <script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="static/js/admin.js"></script>
</head>

<body class="login">
    <div class="login_m">
        <div class="login_logo">
            <img src="/data/images/logo.jpg" width="266" height="66">
        </div>
        <div class="login_boder">
            <div class="login_padding" id="login_model">
                <form id="login_form" method="POST">
                    <div class="username">
                        <h2>USERNAME</h2>
                        <label>
                            <input type="text" id="username" name="username" class="txt_input"
                                placeholder="your username">
                        </label>
                        <span class="uspan"></span>
                    </div>
                    <div class="password">
                        <h2>PASSWORD</h2>
                        <label>
                            <input type="password" id="userpwd" name="pwd" class="txt_input" placeholder="the password">
                        </label>
                        <span class="pspan"></span>
                    </div>
                    <div class="verify">
                        <h2>CAPTCHA </h2>
                        <label>
                            <input type="text" id="verify" name="verify" class="verify_input" placeholder="the captcha"
                                onkeydown=KeyDown()>
                            <img src="" id="verify_img" class="verify_img" width="120px" height="40px"
                                onclick="getCaptchaSrc();">
                            <input type="hidden" name="uuid" id="uuid" value="">
                        </label>
                        <label>
                            <span class="vspan"></span>
                        </label>
                    </div>
                    <div class="forgot_password">
                        <h2>
                            <a href="#">Forgot your password?</a>
                            <!-- <a href="/getpwd.html">Forgot your password?</a> -->
                        </h2>
                    </div>
                    <div class="button">
                        <label>
                            <input type="button" class="sub_button" id="button" value="登 陆">
                        </label>
                    </div>
                </form>
            </div>
            <!--login_padding  Sign up end-->
        </div>
        <!--login_boder end-->
        <div class="copyrights">Collect from
            <a href="#"> huwhois@gmail.com </a>
        </div>
    </div>
    <!--login_m end-->

    <script type="text/javascript">
        function getCaptchaSrc() {
            $.get("/ajax/captcha/captcha_src", null, function (data) {
                // console.log(data.data.codeuid);
                $("#verify_img").attr("src", data.data.base64);
                $("#uuid").val(data.data.codeuid);
            }, "json");
        }
        getCaptchaSrc();
        var userId = "";
        var token = "";
        if (!window.localStorage) {
            alert("此浏览器不适用本系统, 推荐使用Chrome浏览器");
        } else {
            var storage = window.localStorage;
            userId = storage.getItem("userId")? storage.getItem("userId"): "";
            token = storage.getItem("token")? storage.getItem("token"): "";
        }

        if(!isNull(token) || !isNull(userId)) {
            // console.log(userId);
            // console.log(token);
            $.post('/ajax/login/islogin', {
                "userId": userId,
                "token": token,
            }, function (data) {
                // console.log(data);
                if (data==true) {
                    window.location.href="/console/index.html";
                }
                return false;
            }, "json");
        }

        function getCaptchaSrc() {
            $.get("/ajax/captcha/captcha_src", null, function (data) {
                // console.log(data.data.codeuid);
                $("#verify_img").attr("src", data.data.base64);
                $("#uuid").val(data.data.codeuid);
            }, "json");
        }

        $(function () {
            $('#button').css('opacity', 0.7).hover(function () {
                $(this).stop().fadeTo(650, 1);
            }, function () {
                $(this).stop().fadeTo(650, 0.7);
            });

            var issubmit = true;

            $("#username").blur(function () {
                var uname = $(this).val();
                issubmit = checkEmpty(uname, '.uspan', '* 用户名不能为空');
            });

            $("#userpwd").blur(function () {
                var upwd = $(this).val();
                issubmit = checkEmpty(upwd, '.pspan', '* 密码不能为空');
            });

            $("#button").click(function () {
                var uname = $("#username").val();
                var upwd = $("#userpwd").val();
                var verify = $("#verify").val();
                var uuid = $("#uuid").val();
                // console.log(uuid);
                issubmit = checkEmpty(uname, '.uspan', '* 用户名不能为空');
                if (!issubmit) return false;
                issubmit = checkEmpty(upwd, '.pspan', '* 密码不能为空');
                if (!issubmit) return false;
                issubmit = checkEmpty(verify, '.vspan', '* 验证不能为空');
                if (!issubmit) return false;

                $.post('/ajax/login/login', {
                    "username": uname,
                    "password": upwd,
                    "captcha": verify,
                    "uuid": uuid
                }, function (data) {
                    // console.log(data);
                    // return false;
                    if (data.success==true) {
                        var storage = window.localStorage;
                        storage.removeItem("edatamenu");    // 删除之前登陆的目录
                        storage.setItem("userInfo", JSON.stringify(data.data));
                        storage.setItem("userId", data.data.userId);
                        storage.setItem("token", data.data.token);
                        window.location.href="/console/index.html";
                    } else {
                        $(".vspan").html(data.msg);
                    }
                }, "json");
                return false;
            });
        });

        function KeyDown() {
            if (event.keyCode == 13) {
                event.returnValue = false;
                event.cancel = true;
                $("#button").click();
            }
        }
    </script>

</body>

</html>